using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;
public partial class Admin_DonHangDanhSach : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            LoadDropDownList();
        }
    }
    protected void LoadDropDownList()
    {
        tbDonHangCollection dhCollection = tbDonHangBUS.GetAll();
        ddl_madonhang.DataTextField = tbDonHangColumns.MaDH.ToString();
        ddl_madonhang.DataValueField = tbDonHangColumns.MaDH.ToString();
        ddl_madonhang.DataSource = dhCollection;
        ddl_madonhang.DataBind();
        ddl_madonhang.Items.Insert(0, new ListItem("All", "0"));

        tbKhachHangCollection khCollection = tbKhachHangBUS.GetAll();
        ddl_khachhang.DataTextField = tbKhachHangColumns.HoTen.ToString();
        ddl_khachhang.DataValueField = tbKhachHangColumns.MaKH.ToString();
        ddl_khachhang.DataSource = khCollection;
        ddl_khachhang.DataBind();
        ddl_khachhang.Items.Insert(0, new ListItem("All", "0"));

        tbTinhTrangGHCollection ttghCollection = tbTinhTrangGHBUS.GetAll();
        ddl_tinhtranggh.DataTextField = tbTinhTrangGHColumns.TenTTGH.ToString();
        ddl_tinhtranggh.DataValueField = tbTinhTrangGHColumns.MaTTGH.ToString();
        ddl_tinhtranggh.DataSource = ttghCollection;
        ddl_tinhtranggh.DataBind();
        ddl_tinhtranggh.Items.Insert(0, new ListItem("All", "0"));

        
    }
    private void LoadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();
        if (ddl_madonhang.SelectedValue != "0")
        {
            c.add(Condition.AND, tbDonHangColumns.MaDH, Condition.LIKE, ddl_madonhang.SelectedValue);
        }
        if (ddl_khachhang.SelectedValue != "0")
        {
            c.add(Condition.AND, tbDonHangColumns.KhachHang, Condition.LIKE, ddl_khachhang.SelectedValue);
        }
        if (ddl_tinhtranggh.SelectedValue != "0")
        {
            c.add(Condition.AND, tbDonHangColumns.TinhTrangGH, Condition.LIKE, ddl_tinhtranggh.SelectedValue);
        }
        int total = 0;
        tbDonHangCollection dhCollection = tbDonHangBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
            tbDonHangColumns.MaDH.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = dhCollection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
        {
            Pager.Visible = false;
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            tbDonHang donhang = (tbDonHang)e.Item.DataItem;
            HyperLink hyp_MaDH = (HyperLink)e.Item.FindControl("hyp_MaDH");
            Literal lit_KhachHang = (Literal)e.Item.FindControl("lit_KhachHang");
            Literal lit_NgayDatHang = (Literal)e.Item.FindControl("lit_NgayDatHang");
            Literal lit_NgayGiaoHang = (Literal)e.Item.FindControl("lit_NgayGiaoHang");
            Literal lit_TriGia = (Literal)e.Item.FindControl("lit_TriGia");
            Literal lit_TinhTrangGH = (Literal)e.Item.FindControl("lit_TinhTrangGH");
            Literal lit_PhuongThucThanhToan = (Literal)e.Item.FindControl("lit_PhuongThucThanhToan");
            Literal lit_TinhTrangThanhToan = (Literal)e.Item.FindControl("lit_TinhTrangThanhToan");
            Literal lit_NhanVienGiaoHang = (Literal)e.Item.FindControl("lit_NhanVienGiaoHang");
            Literal lit_DiaChiGH = (Literal)e.Item.FindControl("lit_DiaChiGH");
            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/Admin/DonHangUpdate.aspx?iD=" + donhang.MaDH.ToString();
            tbKhachHang khachhang = tbKhachHangBUS.GetByID(donhang.KhachHang);
            lit_KhachHang.Text = khachhang.HoTen;
            tbTinhTrangGH tinhtranggiaohang = tbTinhTrangGHBUS.GetByID(donhang.TinhTrangGH);
            lit_TinhTrangGH.Text = tinhtranggiaohang.TenTTGH;
            tbPhuongThucThanhToan phuongthucthanhtoan = tbPhuongThucThanhToanBUS.GetByID(donhang.PhuongThucThanhToan);
            lit_PhuongThucThanhToan.Text = phuongthucthanhtoan.TenPTTT;
            tbTinhTrangThanhToan tinhtrangthanhtoan = tbTinhTrangThanhToanBUS.GetByID(donhang.TinhTrangThanhToan);
            lit_TinhTrangThanhToan.Text = tinhtrangthanhtoan.TenTTTT;
            tbNhanVien nhanvien = tbNhanVienBUS.GetByID(donhang.NhanVienGiaoHang);
            lit_NhanVienGiaoHang.Text = nhanvien.TenNV;
            tbDiaDiem diadiem = tbDiaDiemBUS.GetByID(donhang.DiaChiGH);
            lit_DiaChiGH.Text = diadiem.DiaChi;

            hyp_MaDH.Text = donhang.MaDH.ToString();
            hyp_MaDH.NavigateUrl = "~/Admin/DonHangChiTiet.aspx?iD=" + donhang.MaDH.ToString();
            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");
            hf_id.Value = donhang.MaDH.ToString();
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }

    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int iD = int.Parse(hf_id.Value);
                tbDonHang donhang = tbDonHangBUS.GetByID(iD);
                tbDonHangBUS.Delete(iD);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
    protected void Pager_Command1(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();


    }
}



